package com.ls.controller;

import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("getCurrentUser")
    public Object getCurrentUser(Authentication authentication) {
        return authentication.getPrincipal();
    }


    @RequestMapping("getUserInfo")
    public Object getCurrentUser(Authentication authentication,HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        //Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJleHAiOjE2Mzg1MjYyNDksImF1dGhvcml0aWVzIjpbImFkbWluIiwiUk9MRV9hYmMiLCIvaW5zZXJ0IiwiL3NlbGVjdCJdLCJqdGkiOiIzMjZhMjY4Mi04OWNjLTRkOWItYmI5MS02ZWZmYzc2ZWJiYTIiLCJjbGllbnRfaWQiOiJjbGllbnQiLCJlbmhhbmNlIjoiZW5oYW5jZSBpbmZvIn0.pofjjzC3vsBP_RnleOlla-pxWnrFHFn8tXb_s0bY6nw
        String token = header.substring(7);
        System.out.println(header);
        //---JwtTokenStoreConfig这设置的签名密钥----
        return Jwts.parser().setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parse(token).getBody();
    }
}
